﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HotelManager.Business_Logic_Layer;
using HotelManager.Data_Transfer_Layer;

namespace HotelManager
{
    public partial class QuanLyKhachHang : UserControl
    {
        khachhangBLO kh_blo;
        int option;
        public int truyendata_option
        {
            set { option = value; }
        }

        public QuanLyKhachHang()
        {
            InitializeComponent();
            kh_blo = new khachhangBLO();
            option = 0;
            Load_GridView();
        }

        public void Load_GridView()
        {
            khachhangDTO[] list_kh = new khachhangDTO[kh_blo.get().Count()];
            list_kh = kh_blo.get();
            // DataTable
            DataTable dt = new DataTable();
            DataColumn tenkh = new DataColumn();
            tenkh.ColumnName = "tenkh";
            tenkh.ReadOnly = true;
            DataColumn cmnd = new DataColumn();
            cmnd.ColumnName = "cmnd";
            cmnd.ReadOnly = true;
            DataColumn tongsongayo = new DataColumn();
            tongsongayo.ColumnName = "tongsongayo";
            tongsongayo.ReadOnly = true;
            DataColumn tongsotienphong = new DataColumn();
            tongsotienphong.ColumnName = "tongsotienphong";
            tongsotienphong.ReadOnly = true;
            DataColumn tongsotiendv = new DataColumn();
            tongsotiendv.ColumnName = "tongsotiendv";
            tongsotiendv.ReadOnly = true;
            DataColumn diemcong = new DataColumn();
            diemcong.ColumnName = "diemcong";
            diemcong.ReadOnly = true;
            DataColumn loaikh = new DataColumn();
            loaikh.ColumnName = "loaikh";
            loaikh.ReadOnly = true;
            DataColumn id = new DataColumn();
            id.ColumnName = "id";
            id.ReadOnly = true;
            //
            dt.Columns.Add(tenkh);
            dt.Columns.Add(cmnd);
            dt.Columns.Add(tongsongayo);
            dt.Columns.Add(tongsotienphong);
            dt.Columns.Add(tongsotiendv);
            dt.Columns.Add(diemcong);
            dt.Columns.Add(loaikh);
            dt.Columns.Add(id);
            //
            foreach (khachhangDTO obj in list_kh)
            {
                DataRow row = dt.NewRow();
                row["tenkh"] = obj.Kh_hoten;
                row["cmnd"] = obj.Kh_cmnd;
                row["tongsongayo"] = obj.Kh_tongsongayo;
                row["tongsotienphong"] = obj.Kh_tongsotienphong;
                row["tongsotiendv"] = obj.Kh_tongsotiendv;
                row["diemcong"] = obj.Kh_diemcong;
                row["loaikh"] = obj.Kh_loaikh;
                row["id"] = obj.Kh_id;
                dt.Rows.Add(row);
            }
            //
            dgvData.DataSource = dt;

            dgvData.Columns[0].Width = 50;
            dgvData.Columns[1].Width = 50;
            dgvData.Columns[2].HeaderText = "Họ Tên";
            dgvData.Columns[3].HeaderText = "Số CMND";
            dgvData.Columns[4].HeaderText = "Tổng số ngày ở";
            dgvData.Columns[5].HeaderText = "Tổng chi phí trả phòng";
            dgvData.Columns[6].HeaderText = "Tổng chi phí trả dịch vụ";
            dgvData.Columns[7].HeaderText = "Điểm cộng";
            dgvData.Columns[8].HeaderText = "Thẻ";

            dgvData.Columns[9].Visible = false;
        }

        private void bttThemKH_Click_1(object sender, EventArgs e)
        {
            ThemKhachHang frm = new ThemKhachHang();
            frm.Show();
        }

        private void dgvData_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                int kh_id = int.Parse(dgvData.Rows[e.RowIndex].Cells[9].Value.ToString());
                khachhangDTO obj = new khachhangDTO();
                obj = kh_blo.View_Detail(kh_id);
                DialogResult result = MessageBox.Show("Bạn có muốn xoá " + obj.Kh_hoten + "?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    MessageBox.Show("Ban chon yes");

                }
                else
                {
                    MessageBox.Show("Ban chon no");
                }

            }
            if (e.ColumnIndex == 1)
            {
                int kh_id = int.Parse(dgvData.Rows[e.RowIndex].Cells[9].Value.ToString());
                khachhangDTO obj = new khachhangDTO();
                obj = kh_blo.View_Detail(kh_id);
                ThemKhachHang frm_themkh = new ThemKhachHang();
                frm_themkh.truyendata_cmnd = obj.Kh_cmnd;
                frm_themkh.truyendata_gioitinh = obj.Kh_gioitinh;
                frm_themkh.truyendata_sodt = obj.Kh_sodt;
                frm_themkh.truyendata_tenkh = obj.Kh_hoten;
                frm_themkh.truyendata_id = kh_id.ToString();
                frm_themkh.truyendata_option = 2;
                frm_themkh.Show();
            }
        }

        private void txtTimKH_TextChanged(object sender, EventArgs e)
        {

        }

        private void bttTimKH_Click(object sender, EventArgs e)
        {
            if (txtTimKH.Text.Length==0)
            {
                MessageBox.Show("Vui lòng nhập tên khách hàng", "Lỗi");
                return;
            }
        }

        
    }
}
